//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CostStructureDao.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:24:44  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.persistence.dao;

import java.sql.Timestamp;
import java.util.Collection;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc01Coststruct;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc02Coststvers;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc03Subcoststr;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc04Subcsthier;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc07Subcstitem;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese02Econsource;
import ar.com.ford.it.webengine.persistence.PersistenceUtil;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.LogFactory;
import com.ford.it.persistence.PersistenceException;
import com.ford.it.persistence.PersistenceManager;
import com.ford.it.persistence.Query;
import com.ford.it.persistence.Transaction;

public class CostStructureDao extends Dao {

    private static final String CLASS_NAME = CostStructureDao.class.getName();
    private static final ILogger log = LogFactory.getInstance().getLogger(
            CLASS_NAME);

    /**
     * 
     * @param costStructureVersion
     * @param status
     * @param lastAppOnly
     * @param dateType
     * @param dateMin
     * @param dateMax
     * @param userActionType
     * @param userId
     * @return
     * @throws PersistenceException
     */
    public Collection findCostStructureVersions(
                                                Teesc02Coststvers costStructureVersion,
                                                String status,
                                                String lastAppOnly,
                                                String dateType,
                                                Timestamp dateMin,
                                                Timestamp dateMax,
                                                String userActionType,
                                                String userId)
            throws PersistenceException {
        String METHOD_NAME = "findCostStructureVersions";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teese02Econsource> resultCol = null;
        PersistenceManager pm = null;
        Object[] parameters = { costStructureVersion, status, lastAppOnly,
                dateType, dateMin, dateMax, userActionType, userId };

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findCostStructureVersions",
                Teesc02Coststvers.class);
        query.setParameterArray(parameters);

        resultCol = (Collection) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return resultCol;
    }

    /**
     * Crea la estructura de costos.
     * 
     * @param costStructure
     * @throws PersistenceException
     */
    public void createCostStructure(Teesc01Coststruct costStructure)
            throws PersistenceException {

        String METHOD_NAME = "createCostStructure";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(costStructure);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * 
     * @param subCostStr
     * @throws PersistenceException
     */
    public void createSubCostStructure(Teesc03Subcoststr subCostStr)
            throws PersistenceException {

        String METHOD_NAME = "createSubCostStructure";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(subCostStr);

        log.exiting(CLASS_NAME, METHOD_NAME);

    }

    public void createCostStructureVersion(Teesc02Coststvers costStrVersion)
            throws PersistenceException {

        String METHOD_NAME = "createCostStructureVersion";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(costStrVersion);

        log.exiting(CLASS_NAME, METHOD_NAME);

    }

    public void createHierarchy(Teesc04Subcsthier hierarchy)
            throws PersistenceException {

        String METHOD_NAME = "createHierarchy";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(hierarchy);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    public void createSubCostStrItem(Teesc07Subcstitem subCostStrItem)
            throws PersistenceException {

        String METHOD_NAME = "createSubCostStrItem";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(subCostStrItem);

        log.exiting(CLASS_NAME, METHOD_NAME);

    }

    /**
     * Busca los subCostStrItem segun Sub Cost Structure y segun si el Item al
     * que estan relacionados requiere o no un Economic Index.
     * 
     * @param subCostStrItem
     * @return
     * @throws PersistenceException
     */
    public Collection<Teesc07Subcstitem> findSubCostStructureItems(
                                                                   Teesc07Subcstitem subCostStrItem)
            throws PersistenceException {

        String METHOD_NAME = "findSubCostStructureItems";
        log.entering(CLASS_NAME, METHOD_NAME);

        Collection<Teesc07Subcstitem> res = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm.newQuery("findSubCostStructureItems",
                Teesc07Subcstitem.class);
        query.setParameters(subCostStrItem);
        res = (Collection<Teesc07Subcstitem>) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }

    /**
     * 
     * @param economicSource
     * @throws PersistenceException
     */
    public void deleteSubCostStrItem(Teesc07Subcstitem subCostStrItem)
            throws PersistenceException {

        String METHOD_NAME = "deleteSubCostStrItem";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(subCostStrItem);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * 
     * @param subCostStr
     * @param costStrVersion
     * @return
     * @throws PersistenceException
     */
    public Teesc04Subcsthier findHierarchyBySon(Teesc03Subcoststr subCostStr,
                                                Teesc02Coststvers costStrVersion)
            throws PersistenceException {
        String METHOD_NAME = "findHierarchyBySon";

        Teesc04Subcsthier res = null;
        PersistenceManager pm = null;

        pm = PersistenceUtil.getPersistenceManager();
        Query query = pm
                .newQuery("findHierarchyBySon", Teesc04Subcsthier.class);
        query.setParameters(subCostStr, costStrVersion);
        res = (Teesc04Subcsthier) pm.executeQuery(query);

        log.exiting(CLASS_NAME, METHOD_NAME);
        return res;
    }

    /**
     * 
     * @param hierarchy
     * @throws PersistenceException
     */
    public void deleteHierarchy(Teesc04Subcsthier hierarchy)
            throws PersistenceException {

        String METHOD_NAME = "deleteHierarchy";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(hierarchy);

        log.exiting(CLASS_NAME, METHOD_NAME);

    }

    /**
     * 
     * @param subCostStr
     * @throws PersistenceException
     */
    public void deleteSubCostStr(Teesc03Subcoststr subCostStr)
            throws PersistenceException {

        String METHOD_NAME = "deleteSubCostStr";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(subCostStr);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * Da de alta la Cost Structure Version pasada por parametro
     * 
     * @param newVersion
     * @throws PersistenceException
     */
    public void createNewCostStructureVersion(Teesc02Coststvers newVersion)
            throws PersistenceException {
        String METHOD_NAME = "createNewCostStructureVersion";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = null;

        tx = PersistenceUtil.getTransaction();
        tx.registerInsert(newVersion);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * 
     * @param costStrVersion
     * @throws PersistenceException
     */
    public void deleteCostStructureVersion(Teesc02Coststvers costStrVersion)
            throws PersistenceException {
        String METHOD_NAME = "deleteCostStructureVersion";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(costStrVersion);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

    /**
     * 
     * @param costStructure
     * @throws PersistenceException
     */
    public void deleteCostStructure(Teesc01Coststruct costStructure)
            throws PersistenceException {
        String METHOD_NAME = "deleteCostStructure";
        log.entering(CLASS_NAME, METHOD_NAME);

        Transaction tx = PersistenceUtil.getTransaction();
        tx.registerDelete(costStructure);

        log.exiting(CLASS_NAME, METHOD_NAME);
    }

}
